// 账务管理
var commonView = require("./commonView.js");
var $accountsDetails, $paramentsInput, $paramentSelect;
var pageNo = 1,
    pageSize = 20;
var startDate, endDate, selectTypeVal = "userPhone";
var maxPage;

function getAccountsRecord() {
    var institution = $.cookie('institution');
    var name = $.cookie('name');
    var password = $.cookie('password');
    var operatorPhone, peratorName, pkgId;
    if (selectTypeVal == "userPhone") {
        operatorPhone = $paramentsInput.val();
    } else if (selectTypeVal == "name") {
        peratorName = $paramentsInput.val();
    } else if (selectTypeVal == "groupName") {
        pkgId = $paramentSelect.find("option:checked").val();
    }
    var paraments = {
        institution: institution,
        pageNo: pageNo,
        pageSize: pageSize,
        startDate: startDate,
        endDate: endDate,
        operatorPhone: operatorPhone,
        operatorName: peratorName,
        pkgId: pkgId
    };

    $.post(queryRecordServlet, paraments, RecordBack);

}

// 取记录列表RecordBack
function getAccounts(argument) {
    $.post(accountServert, {}, function(adata) {
        //  console.dir(adata);
        
        commonView.needLoading(adata);
        if (adata.code == "0000" && adata.data && adata.data.length > 0) {
            for (var i = 0, l = adata.data.length; i < l; i++) {
                var therchrTotalCount = 0,
                    theuseableCount = 0,
                    thequrTotalCount = 0;
                if (adata.data[i].rchrTotalCount != null && adata.data[i].rchrTotalCount != "null" && adata.data[i].rchrTotalCount != "") {
                    therchrTotalCount = adata.data[i].rchrTotalCount;
                }
                if (adata.data[i].useableCount != null && adata.data[i].useableCount != "null" && adata.data[i].useableCount != "") {
                    theuseableCount = adata.data[i].useableCount;
                }
                if (adata.data[i].qurTotalCount != null && adata.data[i].qurTotalCount != "null" && adata.data[i].qurTotalCount != "") {
                    thequrTotalCount = adata.data[i].qurTotalCount;
                }
                if (adata.data[i].pkgId == "001") {
                    $("#rchrTotalCount1").text(therchrTotalCount);
                    $("#useableCount1").text(theuseableCount);
                    $("#qurTotalCount1").text(thequrTotalCount);
                } else if (adata.data[i].pkgId == "002") {
                    $("#rchrTotalCount2").text(therchrTotalCount);
                    $("#useableCount2").text(theuseableCount);
                    $("#qurTotalCount2").text(thequrTotalCount);
                } else if (adata.data[i].pkgId == "003") {
                    $("#rchrTotalCount3").text(therchrTotalCount);
                    $("#useableCount3").text(theuseableCount);
                    $("#qurTotalCount3").text(thequrTotalCount);
                }
            }
        }

    });
}

// 记录回调函数
function RecordBack(tRecordData) {
    // console.dir(tRecordData);
    // console.dir(tRecordData);
    commonView.needLoading(tRecordData);
    var trHtml = '';
    if (tRecordData.totalCount && tRecordData.totalCount != 0) {
        maxPage = tRecordData.totalPage;
        $("#account-nowpage").text(pageNo);
        $("#account-allpage").text(tRecordData.totalPage);
        var totalCount = parseInt(tRecordData.totalCount);
        var RData = tRecordData.data;
        var groupMap = {
            "001": "组合A",
            "002": "组合B",
            "003": "组合C"
        };

        for (var i = 0, l = RData.length; i < l; i++) {
            var labelClass = "success";
            var statemsg = "查询成功";
            if (RData[i].queryStatus != "1") {
                labelClass = "important";
                statemsg = "查询失败";
            }

            var theId = i + 1 + ((pageNo - 1) * pageSize);
            var theDate = new Date(RData[i].insertime);
            var theDateformate = commonView.getDateFormate("yyyy-mm-dd hh:ii:ss", theDate);
            var groupName = groupMap[RData[i].pkgId];
            var thePhone = "";
            if(RData[i].phone && RData[i].phone != "null" && RData[i].phone != "undefined"){
                thePhone = RData[i].phone;
            }
            trHtml += `<tr><td>${theId}</td><td>${theDateformate}</td><td>${groupName}</td><td>${RData[i].operatorName}</td><td>${thePhone}</td><td><span class="label label-${labelClass}">${statemsg}</span></td></tr>`;
        }
        $accountsDetails.find("tbody").html(trHtml);
    } else {
        maxPage = 0;
        $accountsDetails.find("tbody").html("<tr><td><p>没有查询记录</p></td></tr>");
    }

}


// 初始化帐务管理
function initAccouts() {
    var $selectType = $("#accounts-select-type");
    $("#account-queryBtn").on("click", function() {
        $("#account-allpage").text("1");
        $("#account-nowpage").text("1");
        pageNo = 1;
        getAccountsRecord();
    });
    $selectType.on("change", function() {
        $paramentsInput.val("");
        selectTypeVal = $selectType.find("option:checked").val();
        if (selectTypeVal == "groupName") {

            $paramentsInput.hide();
            $paramentSelect.show();
        } else {

            $paramentsInput.show();
            $paramentSelect.hide();
        }
    });

    var $accountStartDate = $("#account-start_date").datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        minView: 2,
        showMeridian: 1,
        language: "cn"
    });

    var $accountendDate = $("#account-end_date").datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        minView: 2,
        showMeridian: 1,
        language: "cn"
    });
    $accountendDate.on("changeDate", function(ev) {
        //  console.log(ev.date.valueOf());
        if (ev.date) {
            endDate = $("#account-end_date").datetimepicker("getDate").setHours(23,59,59,900);
        } else {
            endDate = "";
        }

    });
    $accountStartDate.on("changeDate", function(ev) {
        //  console.log(ev.date.valueOf());
        if (ev.date) {
            startDate = $("#account-start_date").datetimepicker("getDate").setHours(0,0,0,1);
        } else {
            startDate = "";
        }
    });


    $("#account-frist_page").on("click", function() {
        if (1 != pageNo) {
            pageNo = 1;
            getAccountsRecord();
        }
    });
    $("#account-up_page").on("click", function() {
        if (pageNo > 1) {
            pageNo--;
            getAccountsRecord();
        }
    });
    $("#account-next_page").on("click", function() {
        if (maxPage > pageNo) {
            pageNo++;
            getAccountsRecord();
        }
    });
    $("#account-last_page").on("click", function() {
        if (maxPage != pageNo) {
            pageNo = maxPage;
            getAccountsRecord();
        }
    });

}


// 初始化变量
(function initaccValue() {
    $accountsDetails = $("#accounts-details");

    $paramentsInput = $("#accounts-number .col-my-4 input.form-input");
    $paramentSelect = $("#accounts-number .col-my-4 select.form-input");

})();



module.exports = {
    getAccountsRecord: getAccountsRecord,
    getAccounts: getAccounts,
    initAccouts: initAccouts
}